<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../../include.inc.jsp"%>
<div class="row">
    <div class="col-lg-12">
        <form id="formDlg" class="form-horizontal" role="form" enctype="multipart/form-data" method="post">
            <input type="hidden" id="id" name="id" value="${foodMenu.id}" />
            <!-- 数据权限 -->
            <input type="hidden" id="organizationId" name="organization.id" value="${organizationId}" /> 
            <div class="form-group">
                <label class="col-lg-2 control-label">所属商家:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="hidden" class="form-control" id="foodSellerId" name="foodSeller.id" value="${foodMenu.foodSeller.id}" />
                    <input type="text" readonly="readonly" class="form-control" id="foodSellerName" name="foodSellerName" value="${foodMenu.foodSeller.name}" onclick="selectSeller();"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">菜单名称:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="name" name="name" value="${foodMenu.name}" maxlength="30" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">原价:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="price" name="price" value="${foodMenu.price}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">折扣价:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="discount" name="discount" value="${foodMenu.discount}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">材料:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <textarea class="form-control" id="ingredients" name="ingredients" maxlength="225">${foodMenu.ingredients}</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">菜单类型:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <x:dictData dictCode="Menu_Type" var="menuTypes">
                        <x:select var="type" items="${menuTypes}" id="type" name="type" className="form-control">
                            <x:option value="${type.code }" text="${type.name }" selected="${foodMenu.type eq type.code}"></x:option>
                        </x:select>
                    </x:dictData>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">背景图片:</label>
                <div class="col-lg-9" id="bgDiv">
                    <input type="hidden" id="bgName" name="bgName" value="${foodMenu.bgName}"/>
                    <input type="hidden" id="bgImg" name="bgImg" value="${foodMenu.bgImg}"/>
                    <input type="hidden" id="bgUrl" name="bgUrl" value="${foodMenu.bgUrl}"/>
                    <c:if test="${foodMenu.bgImg == '' || foodMenu.bgImg == null}">
                        <input type="file" id="bgImgFile" name="bgImgFile" />
                    </c:if>
                    <c:if test="${foodMenu.bgImg != '' && foodMenu.bgImg != null}">
						<ul class="attachment-list pull-left list-unstyled">
						    <li>
                                <a href="#" class="attached-file" title="背景图片附件">
                                    <i class="ace-icon fa fa-file-o bigger-100"></i>
                                    <span>${foodMenu.bgName}</span>
                                </a>
                                <span class="action-buttons">
                                    <a href="#" onclick="downloadImg('${foodMenu.id}','1');" title="预览">
                                        <i class="ace-icon fa fa-download bigger-120 blue"></i>
                                    </a> 
                                    <a href="#" onclick="deleteImg(this,'${foodMenu.id}','1','bgImgFile','bgDiv');" title="删除">
                                        <i class="ace-icon fa fa-trash-o bigger-120 red"></i>
								    </a>
                                </span>
                            </li>
						</ul>
                    </c:if>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">焦点图片:</label>
                <div class="col-lg-9" id="focusDiv">
                    <input type="hidden" id="focusName" name="focusName" value="${foodMenu.focusName}"/>
                    <input type="hidden" id="focusImg" name="focusImg" value="${foodMenu.focusImg}"/>
                    <input type="hidden" id="focusUrl" name="focusUrl" value="${foodMenu.focusUrl}"/>
                    <c:if test="${foodMenu.focusImg == '' || foodMenu.focusImg == null}">
	                    <input type="file" id="focusImgFile" name="focusImgFile"/>
                    </c:if>
                    <c:if test="${foodMenu.focusImg != '' && foodMenu.focusImg != null}">
                         <ul class="attachment-list pull-left list-unstyled">
                            <li>
                                <a href="#" class="attached-file" title="焦点图片附件">
                                    <i class="ace-icon fa fa-file-o bigger-100"></i>
                                    <span>${foodMenu.focusName}</span>
                                </a>
                                <span class="action-buttons">
                                    <a href="#" onclick="downloadImg('${foodMenu.id}','2');" title="下载">
                                        <i class="ace-icon fa fa-download bigger-120 blue"></i>
                                    </a> 
                                    <a href="#" onclick="deleteImg(this,'${foodMenu.id}','2','focusImgFile','focusDiv');" title="删除">
                                        <i class="ace-icon fa fa-trash-o bigger-120 red"></i>
                                    </a>
                                </span>
                            </li>
                        </ul>
                    </c:if>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">角标图片:</label>
                <div class="col-lg-9" id="cornerDiv">
                    <input type="hidden" id="cornerName" name="cornerName" value="${foodMenu.cornerName}"/>
                    <input type="hidden" id="cornerImg" name="cornerImg" value="${foodMenu.cornerImg}"/>
                    <input type="hidden" id="cornerUrl" name="cornerUrl" value="${foodMenu.cornerUrl}"/>
                    <c:if test="${foodMenu.cornerImg == '' || foodMenu.cornerImg == null}">
	                    <input type="file" id="cornerImgFile" name="cornerImgFile"/>
                    </c:if>
                    <c:if test="${foodMenu.cornerImg != '' && foodMenu.cornerImg != null}">
                         <ul class="attachment-list pull-left list-unstyled">
                            <li>
                                <a href="#" class="attached-file" title="角标图片附件">
                                    <i class="ace-icon fa fa-file-o bigger-100"></i>
                                    <span>${foodMenu.cornerName}</span>
                                </a>
                                <span class="action-buttons">
                                    <a href="#" onclick="downloadImg('${foodMenu.id}','3');" title="下载">
                                        <i class="ace-icon fa fa-download bigger-120 blue"></i>
                                    </a> 
                                    <a href="#" onclick="deleteImg(this,'${foodMenu.id}','3','cornerImgFile','cornerDiv');" title="删除">
                                        <i class="ace-icon fa fa-trash-o bigger-120 red"></i>
                                    </a>
                                </span>
                            </li>
                        </ul>
                    </c:if>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">是否推荐:</label>
                <div class="col-lg-9">
                    <x:dictData dictCode="Is_Recommend" var="isRecommends">
                        <x:select var="isRecommend" items="${isRecommends}" id="isRecommend" name="isRecommend" className="form-control">
                            <x:option value="${isRecommend.code }" text="${isRecommend.name }" selected="${foodMenu.isRecommend eq isRecommend.code}"></x:option>
                        </x:select>
                    </x:dictData>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">优先级:</label><span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="number" min="0" class="form-control" id="priority" name="priority" value="${foodMenu.priority}" />
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript" src="${contextPath}/static/assets/js/jquery.hotkeys.js"></script>
<script type="text/javascript" src="${contextPath}/static/assets/js/bootstrap-wysiwyg.js"></script>
<script type="text/javascript">
function selectSeller(){
    var ajaxSelectUrl = contextPath + '/manage/ordering/foodSeller/select';
    showSelectDlg("选择餐饮商家",ajaxSelectUrl,["table-data1"],setSelectValue);
}
function setSelectValue(obj){
    $("#foodSellerId").val(obj[0].id);
    $("#foodSellerName").val(obj[0].name);
}
jQuery(function($) {
	initFileUpLoad("formDlg");//上传组件初始化
    
    $('#formDlg').validate({
        errorElement : 'span',
        errorClass : 'help-block',
        focusCleanup : false,
        focusInvalid : false,
        onsubmit : false,
        rules : {
            "foodSellerName":{required:true},
            "name":{required:true},
            "price":{required:true,number:true},
            "discount":{required:true,number:true},
            "ingredients":{required:true},
            "type":{required:true},
            "isRecommend":{required:true},
            "priority":{required:true}
        },
        messages : {
            "foodSellerName" : '请选择所属餐饮商家',
            "name":"请输入菜单名称",
            "price":"原价有误",
            "discount":"折扣价有误",
            "ingredients":"请输入材料",
            "type":"请选择菜单类型",
            "isRecommend":"请选择是否推荐",
            "priority":"请输入优先级",
        },
        highlight : function(e) {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },
        success : function(e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-success');
            $(e).remove();
        },
    });
});

/**
 * 下载附件
 */
function downloadImg(id,type) {
    var url = contextPath + "/manage/ordering/foodMenu/downloadImg";
    var inputs = "<input name=\"id\" value=\""+id+"\"/>" + "<input name=\"type\" value=\""+type+"\"/>";
    jQuery('<form action="'+ url +'" method="post">' + inputs  + '</form>').appendTo('.row').submit().remove();
}
/**
 * 删除附件，根据path真实路径删除文件，并且更新实体
 */
function deleteImg(obj,id,type,fileInputName,divId){
    if (confirm("你确认要删除该附件吗？")) {
        $.ajax({
            url : contextPath + "/manage/ordering/foodMenu/deleteImg",
            type : "post",
            data : {
                'id' : id,
                'type':type
            },
            success : function() {
                CmMsg.info("删除封面成功", 0);//提示消息
				$(obj).parent().parent().parent().remove();
                if(type == 1){
                    $("#bgName").val("");
                    $("#bgImg").val("");
                    $("#bgUrl").val("");
                }
                if(type == 2){
                    $("#focusName").val("");
                    $("#focusImg").val("");
                    $("#focusUrl").val("");
                }
                if(type == 3){
                    $("#cornerName").val("");
                    $("#cornerImg").val("");
                    $("#cornerUrl").val("");
                }
                
                addFileUpload(divId,fileInputName);
            }
        });
    }
}
</script>